<template>
  <div class="tree_box">
    <tree-item :data="departmentData" @changeId="change" @resetName="resetName" @changeIsOpenAndSelect="changeIsOpenAndSelect"></tree-item>
  </div>
</template>

<script>
import treeItem from './tree-item';
export default {
  components: {
    treeItem,
  },
  props: {
    propsLeader: {
      type: String,
      default: '',
    },
    defDepartmentId: {
      type: [String, Number],
    },
  },
  data() {
    return {
      departmentData: [],
      count: 0,
      depId: '',
      topLevelName: '',
      initSelectName: '',
      hasFindName: false,
    };
  },
  methods: {
    returndepartmentData() {
      this.departmentData[0].name = this.topLevelName;
      return this.departmentData;
    },
    searchName(name) {
      this.changeIsOpenAndSelect('search', null, name);
    },
    recursionFn(arr) {
      let checked = false;
    //   console.log(this.defDepartmentId, this.count);
      arr.map((item, index) => {
        item.isOpen = false;
        if (item.parentId === '-1') {
          // 第一层默认展开
          item.isOpen = true;
          this.topLevelName = item.name;
        }
        this.count++;
        if (!this.defDepartmentId && this.count == 1 && index == 0) {
          item.isOpen = false;
          item.topLevel = true;
          this.topLevelName = item.name;
        } else if (this.defDepartmentId && this.defDepartmentId == item.id) {
          item.isOpen = true;
          checked = true;
        //   console.log(this.defDepartmentId, item.name);
        }
        if (item.userList && item.userList.length !== 0) {
          item.userList.map((listItem) => {
            listItem.selected = false;
            if (this.propsLeader && listItem.id == this.propsLeader) {
              listItem.selected = true;
              if (item.parentId !== '-1') {
                //说明不是顶级，有父级
                this.initSelectName = listItem.name;
              } else {
                // 是顶级，没有父级
                console.log(listItem.name);
                item.name = `${this.topLevelName}（${listItem.name}）`;
              }
            }
          });
        }
        if (item.children && item.children) {
          let res = this.recursionFn(item.children);
          if (item.parentId !== '-1') {
            checked = item.isOpen || res.checked;
            item.isOpen = checked;
            // console.log(item.name, res.checked);
          }
        }
      });
      return { arr, checked };
    },

    treeFind(tree, func) {
      for (const data of tree) {
        if (func(data)) return data;
        if (data.children && data.children.length) {
          const res = this.treeFind(data.children, func);
          if (res) return res;
        }
      }
      return null;
    },
    recursionChangeIsOpenAndSelect(arr, type, id, name) {
      top: for (let item of arr) {
        if (item.id == id) {
          if (type == 'isOpen') {
            item.isOpen = !item.isOpen;
          }
        }
        if (type == 'resetSelected') {
          if (item.userList && item.userList.length !== 0) {
            item.userList.map((listItem) => {
              listItem.selected = false;
            });
          }
        }
        if (type == 'selected') {
          if (item.userList && item.userList.length !== 0) {
            item.userList.map((listItem) => {
              listItem.selected = false;
              if (listItem.id == id) {
                listItem.selected = true;
                this.departmentData[0].name = `${this.topLevelName}（${listItem.name}）`;
                this.departmentData[0].isOpen = false;
              }
            });
          }
        }

        if (type == 'search') {
          if (item.userList && item.userList.length !== 0) {
            for (const listItem of item.userList) {
              listItem.selected = false;
              if (listItem.name == name) {
                this.changeIsOpenAndSelect('resetSelected', null, null);
                listItem.selected = true;
                item.isOpen = true;
                this.change(listItem.id, listItem.name);
                this.hasFindName = true;
                this.departmentData[0].name = `${this.topLevelName}（${listItem.name}）`;
                break top;
                break;
              } else {
                // 没有匹配到人
                if (!this.hasFindName) {
                  this.hasFindName = false;
                }
              }
            }
            /* item.userList.map((listItem) => {
                            listItem.selected = false
                            if (listItem.name == name) {
                                listItem.selected = true
                                item.isOpen = true
                                this.change(listItem.id, listItem.name)
                                hasFindName = true
                            } else { // 没有匹配到人
                                if (!hasFindName) {
                                    hasFindName = false
                                }
                            }
                        }) */

            /* if (!hasFindName) {
                            this.$emit('noHasFindName')
                        } */
          }
        }
        if (type == 'isOpen') {
          if (item.parentId == id) {
            item.isOpen = !item.isOpen;
          }
        }
        if (item.children && item.children.length !== 0) {
          this.recursionChangeIsOpenAndSelect(item.children, type, id, name);
        }
      }
      return arr;
    },
    change(id, name) {
      this.$emit('changeId', id, name);
    },
    resetName() {
      this.$emit('resetName');
    },
    changeIsOpenAndSelect(type, id, name) {
      if (type == 'search') {
        this.hasFindName = false;
      }
      if (type == 'selected' || type == 'isOpen') {
        this.$emit('resetName');
      }
      this.departmentData = this.recursionChangeIsOpenAndSelect(this.departmentData, type, id, name);
      if (type == 'search' && !this.hasFindName) {
        this.$emit('noHasFindName');
      }
    },
    getDepartmentList() {
      this.$http(this.$API.getLoadAllUser, {}, true, false, true).then((res) => {
        this.departmentData = this.recursionFn(res).arr;
        if (this.propsLeader && this.initSelectName) {
          this.departmentData[0].name = `${this.topLevelName}（${this.initSelectName}）`;
        }
      });
    },
  },
  mounted() {
    this.getDepartmentList();
  },
};
</script>

<style lang="less"></style>
